﻿
using HandyControl.Controls;
using HandyControl.Data;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using WcsZz.Extend;
using wms_wpf.Apiservice;
using wms_wpf.DialogView;
using wms_wpf.Dto;
using wms_wpf.InPut;
using wms_wpf.Views;
using MessageBox = System.Windows.MessageBox;

namespace wms_wpf.ViewModels
{
   public class BoxConfigViewModel:BindableBase, INavigationAware
    {
        //private BoxConfigView View { get; set; }
        private  IBaseService service {  get; set; }
         private IViewService viewService { get; set; }
        private IEssService essService { get; set; }
        private Prism.Services.Dialogs.IDialogService Dialog;
        public BoxConfigViewModel(Prism.Services.Dialogs.IDialogService dialog) 
         {
            //this.View = view;
            this.Dialog = dialog;
            BoxDtos = new List<BoxDto>();
            service = new BaseService();
            viewService = new ViewService();
            essService = new EssService();
            InPut = new InPut_BaseService6();
            InPut_BaseService2=new InPut_BaseService2();
            InPut.PageIndex = 1;
            InPut.PageSize = 20;
            GetList_BoxsCommand = new DelegateCommand(async()=>await GetList_Boxs());
            PageUpdatedCmd= new DelegateCommand(async()=>await GetList_Boxs());
            IndexCommand = new DelegateCommand<string>(Index);
            ShowDialogCommand = new DelegateCommand<string>(ShowDialog);
            GetList_View_ProportionCommand = new DelegateCommand(GetList_View_Proportion);
            GetList_Boxs();
            GetList_View_Proportion();
           
            EventAggregatorHelper.EventAggregator.ResgiterMessage(async arg =>
            {
                if (!string.IsNullOrEmpty(arg.Message))
                {
                    string[] parts=arg.Message.Split('-');
                    await UpdateBox(parts[0], parts[1]);
                    await GetList_Boxs();
                }
            }, "Dialog3");
            EventAggregatorHelper.EventAggregator.ResgiterMessage(async arg =>
            {

                GetList_Boxs();

            }, "GetList_Boxs");
            EventAggregatorHelper.EventAggregator.ResgiterMessage(async arg =>
            {

               
            }, "OneTouch");
        
        }
        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService6 inPut;
        public InPut_BaseService6 InPut
        {
            get { return inPut; }
            set { inPut = value;  RaisePropertyChanged(); }
        }
        private List< BoxDto> boxDtos;
        public List<BoxDto> BoxDtos
        {
            get { return boxDtos; }
            set { boxDtos = value;RaisePropertyChanged(); }
        }
        public DelegateCommand GetList_BoxsCommand { get;set; }
        public DelegateCommand PageUpdatedCmd { get; set; }
        public DelegateCommand UpdateBoxCommand { get; set; }
        public DelegateCommand GetList_View_ProportionCommand { get; set; }
        public DelegateCommand<string> IndexCommand { get; set; }
        public DelegateCommand<string> ShowDialogCommand { get; set; }

        private bool isrunning;
        public bool Isruning
        {
            get { return isrunning; }
            set { isrunning = value; RaisePropertyChanged(); }
        }
        private int selectIndex;
        public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }
        private List<View_ProportionDto> view_ProportionDtos;
        public List<View_ProportionDto> View_ProportionDtos
        {
            get { return view_ProportionDtos; }
            set { view_ProportionDtos = value;RaisePropertyChanged(); }
        }
        private string boxcode;
        public string BoxCode
        {
            get { return boxcode; }
            set { boxcode = value;RaisePropertyChanged(); }
        }
        private string boxgridcount;

        public string BoxGridCount
        {
            get { return boxgridcount; }
            set { boxgridcount = value; RaisePropertyChanged(); }
        }

        private Visibility visibility = Visibility.Hidden;
        public Visibility Visibility
        {
            get { return visibility; }
            set { visibility = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService2 inPut_BaseService2;
        public InPut_BaseService2 InPut_BaseService2
        {
            get { return inPut_BaseService2; }
            set { inPut_BaseService2 = value;RaisePropertyChanged(); }

        }
        string BoxCodeQuery;
        private async Task GetList_Boxs()
        {
            try
            {


                BoxCodeQuery = await essService.queryNodeReader();
                //BoxCodeQuery = "A000003222";
                if (!string.IsNullOrEmpty(BoxCodeQuery))
                {
                    InPut.BoxCode = BoxCodeQuery;
                }
 
                BoxDtos.Clear();
                Tuple<List<BoxDto>, long?, long?> tuple = await service.GetList_Boxs(InPut);
                if (tuple == null)
                {
                    return;
          
                }
                BoxDtos = tuple.Item1;
                Count = (long)tuple.Item2;
                PageCount = (long)tuple.Item3;
                ShowDialog(BoxCodeQuery);
                tuple = await service.GetList_Boxs(InPut);
                BoxDtos = tuple.Item1;
                Count = (long)tuple.Item2;
                PageCount = (long)tuple.Item3;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-GetList_Boxs", ex);
            }

        }
        private async void ShowDialog(string obj)
        {
            if (string.IsNullOrEmpty(obj))
            {
                return ;
            }
            //传入键值对参数  
            DialogParameters dialogParameters = new DialogParameters(); 
            dialogParameters.Add("1", obj);
            Dialog.ShowDialog("Dialog3", dialogParameters, (r =>
            {
                var resulit = r.Result;
            }));
         
        }


        //private async Task GetList_Boxs()
        //{
        //    try
        //    {
        //        BoxDtos.Clear();
        //        Tuple<List<BoxDto>, long?, long?> tuple = await service.GetList_Boxs(InPut);
        //        if (tuple == null)
        //        {
        //            return;
        //        }
        //        BoxDtos = tuple.Item1;
        //        Count = (long)tuple.Item2;
        //        PageCount = (long)tuple.Item3;
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //        LognetHelper.WriteLog("系统-GetList_Boxs", ex);
        //    }

        //}
        private async void GetList_View_Proportion()
        {
            InPut_ViewService8 inPut=new InPut_ViewService8();
            inPut.BoxCode = BoxCode;
            inPut.BoxGridCount = BoxGridCount;
            View_ProportionDtos =await viewService.GetList_View_Proportion(inPut);

        }//获取料盒占比
        private async Task UpdateBox(string BoxGridQuantity,string BoxPositionQuantity)
        {
            //try
            //{
            //    if (View.datagrid.SelectedItems.Count > 1)
            //    {
            //        Growl.Info("只能修改一个", "msg");
            //        return;
            //    }
            //    InPut_BaseService2.BoxGridQuantity = BoxGridQuantity;
            //    if (View.datagrid.SelectedItems.Count == 0&&string.IsNullOrEmpty(BoxCodeQuery))
            //    {
            //        return;
            //    }
            //    foreach (BoxDto item in View.datagrid.SelectedItems)
            //    {
            //            InPut_BaseService2.BoxCode = (item.BoxCode);             
            //    }
            //    if (View.datagrid.SelectedItems.Count == 0)
            //    {
            //        InPut_BaseService2.BoxCode = BoxCodeQuery;
            //    }
            //    bool res=await service.Update_Box(InPut_BaseService2);
            //    if (res)
            //    {
            //        await GetList_Boxs();
            //    }
            //}catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //    LognetHelper.WriteLog("系统-UpdateBox", ex);
            //}
   




        }
        private void Index(string obj)
        {
            switch (obj)
            {
                case "TwoPage": SelectIndex = 1; ; break;
                case "OnePage": SelectIndex = 0; ; break;

            }
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

       

        
    }
}
